package com.study.equ;

import com.study.equ.utils.HexStrConver;
import org.junit.jupiter.api.Test;

import java.io.UnsupportedEncodingException;

/**
 * @author zhuzi
 * 2025/1/15 11:24
 */
public class LedDemo {
    @Test
    public void led01() throws UnsupportedEncodingException {
        String param = "欢迎ABC";
        String str = "00 30 32 31 30 31 35 31";
        if (param != null) {
            str = str + HexStrConver.encode(param);
        }
        str = str + "0D";
        System.out.println(param);
        System.out.println(str);
    }

    @Test
    public void led02() throws UnsupportedEncodingException {
        String str = "!#002";
        String param = "欢迎ABC";
        if (param != null && !("").equals(param)) {
        } else {
            param = " ";
        }
        str = str + param;
        str = str + "$$";
        String s = encodeEncoding(str, "GBK");
        System.out.println(param);
        System.out.println(s);
    }

    private static String hexString = "0123456789ABCDEF";

    public static String encodeEncoding(String str, String encoding) throws UnsupportedEncodingException {
        // 根据默认编码获取字节数组
        byte[] bytes = str.getBytes(encoding);
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        // 将字节数组中每个字节拆解成2位16进制整数
        for (int i = 0; i < bytes.length; i++) {
            sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
            sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
            sb.append(" ");
        }
        return sb.toString();
    }
}
